package com.eagle.functioninterface;

import org.junit.Test;

import java.util.Objects;
import java.util.function.BinaryOperator;

/**
 * {@link BinaryOperator} 函数式接口是{@link java.util.function.BiFunction}接口的一个实现类
 * 是为了解决二元同类事物之间转换的问题
 *
 * @author qinlinsen
 */
public class BinaryOperatorTest11 {
    @Test
    public void testBiOperator() {
        //加法运算:
        Integer add = compute(1, 3, (x, y) -> x + y);
        Integer subtract = compute(1, 3, (x, y) -> y - x);
        Integer multiple = compute(5, 6, (x, y) -> x * y);
        Integer divide = compute(6, 6, (x, y) -> x / y);
        System.out.println(String.format("add=%s", add));
        System.out.println(String.format("subtract=%s", subtract));
        System.out.println(String.format("multiple=%s", multiple));
        System.out.println(String.format("divide=%s", divide));

    }

    private <T> T compute(T x, T y, BinaryOperator<T> processor) {
        Objects.requireNonNull(processor);
        T result = processor.apply(x, y);
        return result;
    }
}
